const app = getApp()
 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 左侧的菜单数据
    leftMenuList:[],
    // 右侧的问题数据
    rightContent: [],
    // 被点击的左侧菜单
    questions: [],
    currentIndex: 0,
    // 右侧内容滚动条与顶部距离
    scrollTop: 0,
    lastItem: false
  },

   // 左侧菜单的点击事件
   handleItemTap(event){
    const {index} = event.currentTarget.dataset
    let rightContent = this.data.questions[index].children;
    this.setData({
      currentIndex: index,
      rightContent,
       //重新设置右侧内容的scroll-view标签到顶部的距离
       scrollTop: 0
    })
    var lastItem;
    if(index == this.data.leftMenuList.length - 1) {
      lastItem = true;
    } else {
      lastItem = false;
    }
    this.setData({
      currentIndex: index,
       //重新设置右侧内容的scroll-view标签到顶部的距离
       scrollTop: 0,
       lastItem
    })
  },

  initProblems(){
    console.log('查询左侧菜单列表')
    app.sendGetRequest('/community/service/questions/type', (res) => {
      let questions = this.data.questions;
      questions.push({
        id: 0,
        name: '高频问题',
        children: []
      })
      res.data.forEach(item => {
        let type = {
          id: item.id,
          name: item.type,
          children: []
        }
        questions.push(type)
      })
      this.setData({
        questions
      })
      let leftMenuList = this.data.questions.map(v => v.name);
      leftMenuList.push('管理条例')
      this.setData({
        leftMenuList
      })
    }, (err) => {
      console.log(err.errMsg)
    })

    console.log('查询问题列表')
    app.sendGetRequest('/community/service/questions/list', (res) => {
      let top = []
      for(let i = 0; i < 5; i++) {
        top.push({
          label_id: res.rows[i].id,
          click_num: res.rows[i].clickNum,
          problem_name: res.rows[i].title,
          problem_answer: res.rows[i].answer
        })
      }
      this.data.questions[0].children = top
      let rightContent = top;
      this.setData({
        rightContent
      })

      res.rows.forEach(item => {
        this.data.questions.forEach(left => {
          if(left.id == item.typeId) {
            left.children.push({
              label_id: item.id,
              click_num: item.clickNum,
              problem_name: item.title,
              problem_answer: item.answer
            })
          }
        })
      })
    }, (err) => {
      console.log(err.errMsg)
    })
    
    console.log('查询条例信息')
    app.sendGetRequest('/community/service/rules/', (res) => {
      let question = {
        label_id: this.data.questions.length - 1,
        label_name: "管理条例",
        label_pid: 0,
        children: [],
      }
      if(res.data != null) {
        let community = []
        res.data.community.forEach(item => {
          let rule = {
            label_id: item.id,
            problem_name: item.title,
          }
          community.push(rule)
        })
       question.children.push({
        label_id: 0,
        problem_name: "小区级管理条例",
        children: community
       })
        let city = []
        res.data.city.forEach(item => {
          let rule = {
            label_id: item.id,
            problem_name: item.title,
          }
          city.push(rule)
        })
        question.children.push({
          label_id: 1,
          problem_name: "城市级管理条例",
          children: city
        })
        let country = []
        res.data.country.forEach(item => {
          let rule = {
            label_id: item.id,
            problem_name: item.title,
          }
          country.push(rule)
        })
        question.children.push({
          label_id: 2,
          problem_name: "国家级管理条例",
          children: country
        })
        let questions = this.data.questions
        questions.push(question)
        this.setData({
          questions
        })
        console.log(this.data.questions)
      }
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  goToAnswer(e){
    console.log(e)
    var clickNum = e.currentTarget.dataset.question.click_num
    let data = {
      id: e.currentTarget.dataset.question.label_id,
      clickNum: parseInt(clickNum) + 1
    }
    app.sendPutRequest('/community/service/questions', data, (res) => {
      console.log(res)
    }, (err) => {
      console.log(err.errMsg)
    })

    var problem = e.currentTarget.dataset.question.problem_name
    var answer = e.currentTarget.dataset.question.problem_answer
    wx.navigateTo({
      url: "/pages/wyservice/questions/questions?problem=" + problem +  "&answer=" + answer
    })
  },

   kindToggle(e) {
      const id = e.currentTarget.id
      const list = this.data.rightContent
      for (let i = 0, len = list.length; i < len; ++i) {
        if (list[i].label_id == id) {
          list[i].open = !list[i].open
        } else {
          list[i].open = false
        }
      }
     this.setData({
      rightContent: list
     })
   },

  onLoad:function (options) {
    this.initProblems()
  }
})